package com.example.xwweibo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.xw.common.entity.Comment;
import com.example.xw.common.entity.Weibo;
import com.example.xw.common.service.WeiboService;
import com.example.xw.common.vo.Result;
import com.example.xwweibo.mapper.CommentMapper;
import com.example.xwweibo.mapper.WeiboMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController("myWeiboServiceImpl")
public class WeiboServiceImpl implements WeiboService {

    @Autowired
    private WeiboMapper weiboMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Weibo> weiboAdd(String userloginname, String title, String content, String img) {
        Weibo weibo = new Weibo();
        weibo.setUserLoginname(userloginname);
        weibo.setTitle(title);
        weibo.setState(1);
        weibo.setContent(content);
        weibo.setImg(img);
        weibo.setCreateTime(new Date());
        weibo.setReadCount(0);
        weiboMapper.insert(weibo);
        return new Result<>(4000,weibo,"添加成功");
    }

    @Override
    public Result<Weibo> findById(int id) {
        Weibo weibo = weiboMapper.selectById(id);
        return new Result<>(2000,weibo,"查询成功");
    }

    @Override
    public Result<Page<Weibo>> findByUserLoginName(int pageNum, int pageSize, String userloginname, int state) {
        Page<Weibo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Weibo> qw = new QueryWrapper<>();
        qw.lambda().eq(Weibo::getState,state)
                .eq(Weibo::getUserLoginname,userloginname)
                .orderByDesc(Weibo::getCreateTime);
        weiboMapper.selectPage(page,qw);
        return new Result<>(2000,page,"查询成功");
    }

    @Override
    public Result<Page<Weibo>> findList(int pageNum, int pageSize, String findtxt, int state) {
        Page<Weibo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Weibo> qw = new QueryWrapper<>();
        qw.lambda().eq(Weibo::getState,state)
                .like(Weibo::getTitle,findtxt).or().like(Weibo::getContent,findtxt)
                .orderByDesc(Weibo::getCreateTime);
        weiboMapper.selectPage(page,qw);
        return new Result<>(4000,page,"查询成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Integer> updateReadCount(int wbid) {
        Weibo weibo = weiboMapper.selectById(wbid);
        weibo.setReadCount(weibo.getReadCount()+1);
        weiboMapper.updateById(weibo);
        return new Result<>(4000,weibo.getReadCount(),"更新成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Weibo> updateState(int wbid, int state) {
        Weibo wb = weiboMapper.selectById(wbid);
        wb.setState(state);
        weiboMapper.updateById(wb);
        return new Result<>(4000,wb,"更新成功");
    }

    @Override
    public Result<Integer> findCountByUserLoginName(String userloginname, int state) {
        QueryWrapper<Weibo> qw = new QueryWrapper<>();
        qw.lambda().eq(Weibo::getState,state)
                .eq(Weibo::getUserLoginname,userloginname);
        Integer count = weiboMapper.selectCount(qw);
        return new Result<>(2000,count,"查询成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> delete(int id) {
        QueryWrapper<Comment> cmQw = new QueryWrapper<>();
        cmQw.lambda().eq(Comment::getWeiboId,id);
        int count = commentMapper.selectCount(cmQw);
        if (count == 0)
            weiboMapper.deleteById(id);
        else
            this.updateState(id,-1);
        return new Result<>(2000,true,"删除成功");
    }


}
